<template>
    <div class="my-carousel">
    <swiper :indicator-dots="indicatorDots" :autoplay="config.autoplaySpeed?config.autoplaySpeed*1000:3000" :interval="interval"
            :duration="duration">
        <swiper-item v-for="image in images">
            <img @click="imageClick(image)" class="my-img"  slot="img" :src="imageUrl(image.fileId)"/>
        </swiper-item>
    </swiper>
    </div>
</template>

<script>
/**
 * 自定义页面(轮播图组件)
 */
import FormPcApi from "@/api/form/FormPcApi.js"
import InsNewsApi from "@/api/protal/insNews.js"
import TokenUtil from "@/common/TokenUtil";

export default {
    name: "carouselView",
    props: {
        config:{
            type:Object,
            default:{},
        },
    },
    data() {
        return {
            images:[],
            indicatorDots: true,
            autoplay: true,
            interval: 2000,
            duration: 500
        }
    },
    mounted() {
        this.init();
    },
    methods: {
        init() {
            this.images = [];
            if (this.config) {
                //表单
                if (this.config.dataSource == 'form') {
                    this.getCarouselData();
                } else { //新闻公告
                    this.getInsNews();
                }
            }
        },
        getCarouselData() {
            if (this.config.bodefId) {
                let param = {
                    boDefId: this.config.bodefId,
                    maxNum: this.config.maxNum,
                    titleField: this.config.titleField,
                    imgField: this.config.imgField,
                }
                FormPcApi.getCarouselData(param).then(res => {
                    if (res.success && res.data) {
                        this.images = [];
                        for (let i = 0; i < res.data.length; i++) {
                            let obj = res.data[i];
                            let title = obj[this.config.titleField];
                            let fileId = "";
                            let img = obj[this.config.imgField];
                            /**
                             * [{"fileId":"附件ID","fileName":"文件名","size":大小}]
                             */
                            if (img) {
                                let images = JSON.parse(img);
                                fileId = images[0].fileId;
                            }
                            this.images.push({
                                title: title,
                                fileId: fileId,
                                pkId: obj.pkId || obj.PKID
                            })
                        }
                    }
                });
            }
        },
        //新闻公告
        getInsNews() {
            if (!this.config.newType) {
                return;
            }
            InsNewsApi.getAllIssuedNews({
                params: {'Q_SYS_DIC_NEW__S_EQ': this.config.newType},
                pageSize: this.config.maxNum || 10
            }).then(res => {
                if (res.data) {
                    this.images = [];
                    for (let i = 0; i < res.data.length; i++) {
                        let obj = res.data[i];
                        if (!obj.imgFileId) {
                            continue;
                        }
                        this.images.push({
                            title: obj.subject,
                            fileId: obj.imgFileId,
                            pkId: obj.pkId
                        })
                    }
                }
            });
        },
        imageUrl(fileId) {
            let token=TokenUtil.getToken();
            let url = `/api/api-system/system/core/sysFile/previewImg?fileId=${fileId}&accessToken=` + token;
            return url;
        },
        imageClick(imageObj) {
            if (this.config.clickFunc == 'noAction') {
                return;
            }
            if (this.config.dataSource == 'form') {
                this.getFormDetail(imageObj.pkId);
            } else {
                this.getNewsDetail(imageObj.pkId);
            }
        },
        getFormDetail(pkId) {
            let params = {
                action: "detail",
                pk: pkId,
                alias: this.config.alias,
            }
            uni.navigateTo({
                url: '/views/modules/form/customForm/FormSolutionShow/FormSolutionComponent?params=' + JSON.stringify(params)
            })
        },
        //显示消息明细
        getNewsDetail(pkId) {
            uni.navigateTo({
                url: '/views/modules/announcement/announcementDetails?pkId='+pkId
            });
        }
    },
    watch: {
        config:{
            handler: function (val, oldVal) {
                this.init();
            },
            deep: true
        }
    },
}
</script>

<style scoped>
.my-carousel{
    border-radius: .5rem;
    overflow: hidden;
    width: calc(100% - 1.2rem);
    margin: 0.6rem auto;
    box-shadow: 0rem 0rem 0.2rem 0rem #dce0eb;
}
.my-img{
    height: 100%;
    width: 100%;
    object-fit: cover;
    box-sizing: border-box;
}
</style>
